查看原文
其他

2018-05-23

绘制好看的热图(pheatmap

 

pheatmap全称为pretty heamap;

pheatmap(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name =

  "RdYlBu")))(100), kmeans_k = NA, breaks = NA, border_color = "grey60",

  cellwidth = NA, cellheight = NA, scale = "none", cluster_rows = TRUE,

  cluster_cols = TRUE, clustering_distance_rows = "euclidean",

  clustering_distance_cols = "euclidean", clustering_method = "complete",

  clustering_callback = identity2, cutree_rows = NA, cutree_cols = NA,

  treeheight_row = ifelse(cluster_rows, 50, 0),

  treeheight_col = ifelse(cluster_cols, 50, 0), legend = TRUE,

  legend_breaks = NA, legend_labels = NA, annotation_row = NA,

  annotation_col = NA, annotation = NA, annotation_colors = NA,

  annotation_legend = TRUE, drop_levels = TRUE, show_rownames = T,

  show_colnames = T, main = NA, fontsize = 10, fontsize_row = fontsize,

  fontsize_col = fontsize, display_numbers = F, number_format = "%.2f",

  number_color = "grey30", fontsize_number = 0.8 * fontsize,

  gaps_row = NULL, gaps_col = NULL, labels_row = NULL,

  labels_col = NULL, filename = NA, width = NA, height = NA,

  silent = FALSE, ...)


参数较多,但是直接用默认参数就能产生比较好看的热图了;下面以例子的形式给出pheatmap的一些重要参数的用法:

#首先构建一个矩阵用于测试:

test = matrix(rnorm(200), 20, 10)

test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3

test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2

test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4

colnames(test) = paste("Test", 1:10, sep = "")

rownames(test) = paste("Gene", 1:20, sep = "")

#test为20行10列的矩阵:

 

#采取默认参数直接出图


library(pheatmap)

pheatmap(test)


#默认参数下是对行列均进行聚类(可设置cluster_row = FALSE, cluster_col = FALSE不进行行列的聚类;如果进行聚类了,还可以通过设置treeheight_row=0, treeheight_col=0显示dendrogram),矩阵没有进行标准化(标准化参数为scale可选"none", "row", "column"),热图的每个小块之间以灰色隔开(参数border_color,如果不想要border可以设置为NA,当然也可以设置成其它颜色),legend显示在右上方(可设置legend = FALSE不显示legend);热图的颜色可利用参数color调整;

 


 

#可自由设置legend的标签


pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",

"1e-4", "1e-3", "1e-2", "1e-1", "1"))


#在legend上的-1~4位置显示"0", "1e-4", "1e-3", "1e-2", "1e-1", "1"

 

 

#可设置参数display_numbers数值显示在热图的格子中,可通过number_format设置数值的格式,较常用的有"%.2f"(保留小数点后两位)"%.1e"(科学计数法显示,保留小数点后一位)number_color设置显示内容的颜色:

pheatmap(test, display_numbers = TRUE, number_format = "%.2f", number_color="purple") #"%.2f"表示保留小数点后两位

 

pheatmap(test, display_numbers = TRUE, number_format = "%.1e") #"%.1e"表示以科学计数法表示,保留小数点后一位

 

#还可以自己设定要显示的内容;

pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))

 

 


#pheatmap还提供了参数设置每个格子的大小

pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap", fontsize = 8, filename = "test.pdf") #main可设置热图的标题,fontsize设置字体大小,filename可直接将热图存出,支持格式png, pdf, tiff, bmp, jpeg,并且可以通过width, height设置图片的大小;

 

 


#pheatmap还可以显示行列的分组信息,支持多种分组;

annotation_col = data.frame(CellType = factor(rep(c("CT1", "CT2"), 5)), Time = 1:5)

rownames(annotation_col) = paste("Test", 1:10, sep = "")

 

annotation_row = data.frame(GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))))

rownames(annotation_row) = paste("Gene", 1:20, sep = "")

 

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)

 

#还可以自己设定各个分组的颜色

ann_colors = list(Time = c("white", "firebrick"), #连续数值型分组可设置成渐变

    CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),

    GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E"))

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,

         annotation_colors = ann_colors)

 

 


#pheatmap还能够根据特定的条件将热图分隔开

# cutree_rows, cutree_cols:根据行列的聚类数将热图分隔开;

pheatmap(test,cutree_rows=3,cutree_cols=2)

 

#还可以利用gaps_row, gaps_col自己设定要分隔开的位置

pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),

         cutree_col = 2)

 

 


#可以设置labels_row, labels_col自己设定或列的标签

labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Il10", "Il15", "Il1b")

 pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)

 


欢迎关注生信人




    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存